<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2020/07/27
 * Time: 下午5:13
 */
namespace App\Api\Controllers\Zgyd;

use App\Api\Controllers\BaseController;
use App\Common\PaySuccessAction;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use App\Api\Controllers\zgyd\BaseController as base;

class NotifyController extends BaseController{

    public function pay_notify_url(Request $request)
    {
        try {
            $data = $request->all();
            Log::channel("zgyd")->info("notify", [$data]);
            $base = base::getInstance();
            $verify = $base->verify($data);

            if ($verify) {
                $out_trade_no = $data['merTradeNo'];

                $day = date('Ymd', time());
                $table = 'orders_' . $day;
                if (Schema::hasTable($table)) {
                    $order = DB::table($table)->where('out_trade_no', $out_trade_no)->first();
                } else {
                    $order = Order::where('out_trade_no', $out_trade_no)->first();
                }

                //订单存在
                if ($order) {
                    //系统订单未成功
                    if ($order->pay_status == '2') {
                        $trade_no = $data['orgSysTrace'];
                        if ($data['oldPayType'] == 'WEIX') {
                            $trade_no = $data['oldTradeId'];
                        }

                        if ($data['oldPayType'] == 'ZFBA') {
                            $trade_no = $data['oldTradeNo'];
                        }
                        $buyer_user_id = '';


                        $pay_time = $data['bankDate'].$data['bankTime'];
                        $buyer_pay_amount = $data['txnAmt'] * 0.01;

                        $in_data = [
                            'status' => '1',
                            'pay_status' => 1,
                            'pay_status_desc' => '支付成功',
                            'trade_no' => $trade_no,
                            'pay_time' => $pay_time,
                            'buyer_id' => $buyer_user_id,
                            'buyer_pay_amount' => $buyer_pay_amount,
                        ];

                        $this->update_day_order($in_data, $out_trade_no);

                        if (strpos($out_trade_no, 'scan')) {

                        } else {
                            //支付成功后的动作
                            $data = [
                                'ways_type' => $order->ways_type,
                                'company' => $order->company,
                                'ways_type_desc' => $order->ways_type_desc,
                                'source_type' => '120001', //返佣来源
                                'source_desc' => '中国亚大', //返佣来源说明
                                'total_amount' => $order->total_amount,
                                'out_trade_no' => $order->out_trade_no,
                                'other_no' => $order->other_no,
                                'rate' => $order->rate,
                                'fee_amount' => $order->fee_amount,
                                'merchant_id' => $order->merchant_id,
                                'store_id' => $order->store_id,
                                'user_id' => $order->user_id,
                                'config_id' => $order->config_id,
                                'store_name' => $order->store_name,
                                'ways_source' => $order->ways_source,
                                'pay_time' => $pay_time,
                                'device_id' => $order->device_id,
                            ];
                            PaySuccessAction::action($data);
                        }
                        echo 'sucess';
                    }
                }
            }

        } catch (\Exception $ex) {
            echo $ex->getMessage();
        }
    }


    

}
